//获取应用实例
var app = getApp()
Page({
  data: {
    flashPicData:[],
    imgSrc:'',
    contentList:[],
    userId:''
  },
  //选择某一项
  chooseOne:function(e){
    console.log(e.currentTarget.dataset.no);
    wx.navigateTo({
      url: '../content/content?id='+e.currentTarget.dataset.no
    })
  },
    //点赞
  chooseLike:function(e){
    var that = this;
    console.log(e.currentTarget.dataset.no);
    wx.request({
      url:app.URL+'/edit_like_num',
      data:{
        id:e.currentTarget.dataset.no,
        userId:that.data.userId
      },
      success:function(data){
        console.log(data);
        that.fetchData();
      }
    })
  },
    //进入评论
  chooseMark:function(e){
    console.log(e.currentTarget.dataset.no);
    wx.navigateTo({
      url: '../mark/mark?id='+e.currentTarget.dataset.no
    })
  },
  onLoad: function () {
    var that = this;
    that.getOpenID();
    that.fetchData();
    console.log(that.data.flashPicData);
  },
  onShow:function(){
      var that = this;
      var timer;
      that.getOpenID();
      that.fetchData();
  },
  //获取数据
  fetchData:function(){
    var that=this;
    wx.request({
      url:app.URL+'/get_all_info_like',
      success:function(data){
        if(data.statusCode==200){
            console.log(data.data);
            that.setData({
              flashPicData:data.data,
              contentList:data.data
            });
            var length = that.data.flashPicData.length
            var j=0;
            setInterval(function(){
                j++;
                if(j>=length){
                  j=0;
                }
                that.setData({
                    imgSrc:that.data.flashPicData[j].src
                });
            },3000);
            that.setData({
                imgSrc:that.data.flashPicData[0].src
            });
        } else{
          console.log("error")
        }
      }
    });
  },
  //获取openid
  getOpenID:function(){
    var that = this;
    wx.login({
      success: (res)=>{
       // console.log(res.code);
        wx.request({
           url:'https://api.weixin.qq.com/sns/jscode2session?appid=wx43aa02486e6d3f7d&secret=7455a8c932c7b162a9134643379cdc47&js_code='+res.code+'&grant_type=authorization_code',
           success:function(data){
              //console.log(data.data.openid)
              that.setData({
                  userId:data.data.openid
              });
           }
         })
      },
    })
  }
})
